﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using FubiManger.Controls;

namespace FubiManger.UI
{
    partial class AddcouponForm : Smobiler.Core.Controls.MobileForm
    {
        public AddcouponForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 规则ID
        /// </summary>
        private string ruleid = "0";
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 时间单位
        /// </summary>
        private string unit { get; set; }
        ms_merchant merchant = new ms_merchant();
        /// <summary>
        /// 0代表余额不足1代表可以
        /// </summary>
        private int stype = 0;
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        
       
      
        int count = 0;
        /// <summary>
        /// 红包数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtcount_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                count = Convert.ToInt32(txtcount.Text);
                txtcount.Text = count.ToString();
                int money = Convert.ToInt32(txtmoney.Text);
                txtmoney.Text = money.ToString();
                blance = count * money;
                lblmoney.Text = blance.ToString() + "元";
                try
                {
                    if (blance > Convert.ToDouble(merchant.balance))
                    {
                        stype = 0;
                        lbl_blance.Visible = true;
                    }
                    else
                    {
                        stype = 1;
                        lbl_blance.Visible = false;
                    }
                }
                catch (Exception)
                {

                    stype = 0;
                    lbl_blance.Visible = true;
                }

            }
            catch (Exception)
            {

                txtcount.Text = "0";
            }
        }
        int blance = 0;
        private void txtmoney_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                int money = Convert.ToInt32(txtmoney.Text);
                txtmoney.Text = money.ToString();
                blance = count * money;
                lblmoney.Text = blance.ToString() + "元";
                try
                {
                    if (blance > Convert.ToDouble(merchant.balance))
                    {
                        stype = 0;
                        lbl_blance.Visible = true;
                    }
                    else
                    {
                        stype = 1;
                        lbl_blance.Visible = false;
                    }
                }
                catch (Exception)
                {

                    stype = 0;
                    lbl_blance.Visible = true;
                }
            }
            catch (Exception)
            {

                txtmoney.Text = "0";
            }
        }
        string level = "1";
        /// <summary>
        /// 选择等级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panlevel_Press(object sender, EventArgs e)
        {
            PacketlevelForm frm = new PacketlevelForm();
            this.Form.Show(frm, true, (obj, args) =>
            {
                if (!string.IsNullOrEmpty(frm.level))
                {
                    lbllevel.Text = frm.level + "级";
                    level = frm.level;
                }
            });
        }
        /// <summary>
        /// 选择范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panrank_Press(object sender, EventArgs e)
        {
            packetrankcontrol con = new packetrankcontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                lblrank.Text = con.p_rank;//范围
                if (lblrank.Text == "本地")
                {
                    txtrank.Visible = true;
                    lbl_rank.Visible = true;
                }
                else
                {
                    txtrank.Visible = false;
                    lbl_rank.Visible = false;
                }
            });
        }
        /// <summary>
        /// 选择规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panrule_Press(object sender, EventArgs e)
        {
            AddruleForm frm = new AddruleForm();
            this.Form.Show(frm, (obj, args) =>
            {
                if (!string.IsNullOrEmpty(frm.ruleid))
                {
                    lblruletitle.Text = frm.ruletitle;
                    ruleid = frm.ruleid;//规则ID
                }
            });
        }
        /// <summary>
        /// 添加消费券
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pansave_Press(object sender, EventArgs e)
        {
            if (stype == 0)
            {
                MessageBox.Show("您的余额不足请充值");
            }
            else if (ruleid == "0")
            {
                MessageBox.Show("请设置规则");
            }
            else if (DateTime.Compare(dpstart.Value, dpend.Value) < 0)
            {
                MessageBox.Show("活动结束时间需大于开始时间");
            }
            else
            {
                ms_packet packet = new ms_packet();
              //  DateTime starttime = Convert.ToDateTime(lblday.Text + " " + lblhour.Text);//起始时间
                packet.p_starttime = dpstart.Value.ToString();//开始时间
                packet.p_merchantid = merchantid;//商户ID
                packet.p_money = blance.ToString();//红包金额
               // packet.p_often = txtoften.Text;//活动时长
                packet.p_rank = txtrank.Text;//范围距离
                packet.p_stype = "2";//消费券
                packet.p_level = level;//等级
                packet.p_ruleid = ruleid;//规则ID
                packet.p_kettype = "2";
                if (lblrank.Text == "本地")
                    packet.p_way = "2";
                else
                    packet.p_way = "1";                
                packet.p_ketcount = txtcount.Text;//红包个数
                try
                {
                    if (packetDal.Addpacket(packet) > 0)
                    {
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    // Toast("参数异常");
                }
            }
        }

        private void AddcouponForm_Load(object sender, EventArgs e)
        {
            merchant = merchantDal.Querybyid(merchantid);
           // lblday.Text = DateTime.Now.ToString("yyyy/MM/dd");
        }
    }
}